﻿using Common.AspNetCore;
using Common.Infrastructure;
using Log.Api.Extensions;
using Log.Api.LogModels;
using Log.Api.QueryModel;
using Log.Api.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Log.Api.Controllers
{
    [Route("api/EquipmentParamLog")]
    public class EquipmentParamLogController : ApiController
    {
        private EquipmentParamLogService service;

        public EquipmentParamLogController(IServiceProvider provider, EquipmentParamLogService service)
            : base(provider)
        {
            this.service = service;
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="model"></param>
        [HttpPost("AddLogs")]
        public async Task<ApiActionResult<bool>> AddLogs([FromBody] List<EquipmentParamLogModel> model)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await service.InsertsAsync(model);
            });
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost("GetLogPage")]
        public ApiActionResult<ApiPagedResult<EquipmentParamLogModel>> GetLogPage([FromBody] EquipmentParamLogQuery query)
        {
            return SafeExecute<ApiPagedResult<EquipmentParamLogModel>>((param) =>
            {
                ApiPagedResult<EquipmentParamLogModel> ret = new ApiPagedResult<EquipmentParamLogModel>();
                if (query.BeginTime == null)
                {
                    query.BeginTime = DateTime.Now.AddDays(-7).Date;
                }

                if (query.EndTime == null)
                {
                    query.EndTime = DateTime.Now.AddDays(1).Date;
                }

                query.BeginTime = ElasticClientExtension.GetDateForEs(query.BeginTime.Value);
                query.EndTime = ElasticClientExtension.GetDateForEs(query.EndTime.Value);
                ret.Data = service.GetLogPage(query);
                var TotalCount = (int)service.GetTotalCountByPage(query);
                ret.TotalCount = TotalCount < 0 ? 0 : TotalCount;
                return ret;
            });
        }
    }
}
